package com.kpiJavaTraining.bogdan.module2;

public class QuestionOneAnswer extends Question {

	private static final long serialVersionUID = -3595222810397914844L;
	private final String[] answers;
	private final int indexOfCorrectAnswer;
	
	public QuestionOneAnswer(String question, String[] answers , int indexOfCorrectAnswer) {
		super(question);
		this.answers = answers;
		this.indexOfCorrectAnswer = indexOfCorrectAnswer;
	}

	@Override
	public void answer(String answer){
		int result = Integer.valueOf(answer);
		if( result == getCorrectAnswer()){
			setScore(1);
		}
		else this.clear();
	}

	private int getCorrectAnswer() {
		return indexOfCorrectAnswer;
	}

	@Override
	public String toString(){
		StringBuffer string = new StringBuffer();
		String horizontalSeparator = "- ";
		String verticalSeparator = " | ";
		string.append(super.toString());
		string.append('\n');
		int index = 0;
		for(String answer : answers){
			string.append(String.format("%s %s %s %s", answer, horizontalSeparator, index, verticalSeparator));
			index++;
		}
		return string.toString();
	}
}
